package com.zhangyj.impl.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.zhangyj.dao.RoleDao;
import com.zhangyj.db.DBUtil;
import com.zhangyj.entitis.Role;

public class RoleDaoImpl extends IbatisDao implements RoleDao {
	public RoleDaoImpl(SqlSessionFactory factory) {
		super(factory);
	}

	public List<?> getList() throws SQLException {
		return this.selectList("role.selectAll");
	}

	@Override
	public void addRole(Role role) throws SQLException {
		update("role.insert", role);
	}

	@Override
	public void delRole(int roleid) throws SQLException {
		update("role.delByPK", roleid);
	}

	@Override
	public void editRole(Role role) throws SQLException {
		update("role.updateByPK", role);
	}

	public Set<Integer> getUserRole(int userid) throws SQLException {
		String sql = "SELECT roleid FROM userrole WHERE userid=? ";
		Set<Integer> set = new HashSet<Integer>();
		try {
			Connection conn = DBUtil.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setInt(1, userid);
			ResultSet rst = ps.executeQuery();
			while (rst.next()) {
				set.add(rst.getInt(1));
			}
			rst.close();
			ps.close();
		} catch (SQLException e) {
			throw e;
			// TODO: handle exception
		} finally {
			DBUtil.close();
		}
		return set;
	}

	public void setUserRole(int userid, int[] roleids) throws SQLException {
		try {
			Connection dbc = DBUtil.getConnection();
			DBUtil.begnTrans();
			String sql = "DELETE FROM userrole WHERE userid=? ";
			PreparedStatement ps = dbc.prepareStatement(sql);
			ps.setInt(1, userid);
			ps.executeUpdate();
			ps.close();
			if (roleids.length == 0) {
				return;
			}
			sql = "INSERT INTO userrole (userid,roleid) VALUES(?,?) ";
			ps = dbc.prepareStatement(sql);
			for (int i = 0; i < roleids.length; i++) {
				ps.setInt(1, userid);
				ps.setInt(2, roleids[i]);
				ps.addBatch();
				if (i > 0 && i % 20 == 0) {
					ps.executeBatch();
					ps.clearBatch();
				}
			}
			ps.executeBatch();
			ps.clearBatch();
			ps.close();
			ps.close();
			DBUtil.commit();
		} catch (SQLException e) {
			DBUtil.rollback();
			throw e;
			// TODO: handle exception
		} finally {
			DBUtil.close();
		}
	}

	public void delUserRole(int userid, Connection conn) throws SQLException {
		String sql = "DELETE FROM userrole WHERE userid=? ";
		PreparedStatement ps = conn.prepareStatement(sql);
		ps.setInt(1, userid);
		ps.executeUpdate();
		ps.close();
	}

	public int getActions(int userId, int functionId) throws SQLException {
		int actions = 0;
		java.util.Map<String, Integer> map = new java.util.HashMap<String, Integer>();
		map.put("proid", functionId);
		map.put("userid", userId);
		SqlSession sqlSession = null;
		try {
			sqlSession = factory.openSession(true);
			List<?> list = sqlSession.selectList("role.selectActions", map);
			for (Iterator<?> iters = list.iterator(); iters.hasNext();) {
				actions = actions | ((Integer) iters.next()).intValue();
			}
			return actions;
		} finally {
			if (sqlSession != null)
				sqlSession.close();
		}
	}
}
